package com.rd.student.dashuju.zhengruixian.test0803;
import java.util.ArrayList;

public class D1 implements Comparable<D1>{
    public  int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    private int age;
    public D1(int age){
        this.age=age;
    }
    @Override
    public int compareTo(D1 o){
        if (this.age>o.getAge()){
            return 1;
        }
        if (this.age<o.getAge()){
            return -1;
        }
        return 0;
    }

    public static void main(String[] args) {
        ArrayList<D1> data = new ArrayList<>();
        data.add(new D1(20));
        data.add(new D1(50));
        data.add(new D1(25));
        data.add(new D1(18));
        System.out.println("排序前："+data);
        sort(data);

    }
        public static void sort(ArrayList<D1> data) {
            for (int i = 0; i <data.size()-1;i++){
                for (int j=0;j<data.size()-1-i;j++) {
                    if (data.get(j).compareTo(data.get(j + 1)) > 0) {
                        D1 temp = data.get(j);
                        data.set(j, data.get(j + 1));
                        data.set(j + 1, temp);
                    }
                }
                }
            System.out.println("排序后:"+data);
            }




    @Override
    public String toString() {
        return "D{" +
                "age=" + age +
                '}';
    }
}

